﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms.Design;
using System.Xml;
using UmcProvider.Utils;

namespace UmcProvider.Controls
{
    public class UmcDesigner<T> : ControlDesigner
        where T : IUmc
    {
        protected virtual List<String> ExceptionList
        {
            get
            {
                var source = new List<String>();
                source.Add("Name");
                source.Add("Name_0");
                source.Add("Name_1");
                source.Add("Name_2");
                source.Add("Name_3");
                source.Add("Width");
                source.Add("Height");
                return source;
            }
        }

        protected virtual List<String> ExcludedList
        {
            get
            {
                List<String> source = new List<String>();
                source.Add("AccessibleDescription");
                source.Add("AccessibleName");
                source.Add("AccessibleRole");
                source.Add("ImeMode");

                return source;
            }
        }

        protected override void PreFilterProperties(IDictionary properties)
        {
            base.PreFilterProperties(properties);

            // remove non-umc property
            Reflection.RemoveNonUmcProperty<T>(properties, ExceptionList, ExcludedList);
        }
    }

    public class UmcContainerDesigner<T> : System.Windows.Forms.Design.ParentControlDesigner 
        where T : IUmc
    {
        protected virtual List<String> ExceptionList
        {
            get
            {
                var source = new List<String>();
                source.Add("Name");
                source.Add("Name_0");
                source.Add("Name_1");
                source.Add("Name_2");
                source.Add("Name_3");
                source.Add("Width");
                source.Add("Height");
                return source;
            }
        }

        protected virtual List<String> ExcludedList
        {
            get
            {
                List<String> source = new List<String>();
                source.Add("AccessibleDescription");
                source.Add("AccessibleName");
                source.Add("AccessibleRole");
                source.Add("ImeMode");

                return source;
            }
        }

        protected override void PreFilterProperties(IDictionary properties)
        {
            base.PreFilterProperties(properties);

            // remove non-umc property
            Reflection.RemoveNonUmcProperty<T>(properties, ExceptionList, ExcludedList);
        }

        //private void IncludeProperty(IDictionary properties)
        //{
        //    var excludedProperties = properties.Keys.Cast<String>().
        //        Except(ExceptionList).
        //        Except(Reflection.GetPropertieNames<T>()).
        //        ToArray();

        //    foreach (string propertyName in excludedProperties)
        //    {
        //        properties.Remove(propertyName);
        //    }
        //}

        //private void ExceptProperty(IDictionary properties)
        //{
        //    IEnumerable<Type> excludeInterfaces = null;
        //    IEnumerable<String> excludedProperties = null;

        //    excludeInterfaces = Reflection.GetInterfacesIncludeSelf(typeof(T)).
        //        Where(t => t.IsDefined(typeof(ExcludePropertyAttribute), false));
        //    excludedProperties = Reflection.GetPropertieNames(excludeInterfaces);

        //    foreach (string propertyName in excludedProperties)
        //    {
        //        // Remove will do nothing if specified 'propertyName' is not included in 'properties'.
        //        properties.Remove(propertyName);
        //    }
        //}

        //private void RemoveNonUmcProperty(IDictionary properties)
        //{
        //    Type firstExcludeInterface = null;

        //    firstExcludeInterface = Reflection.GetInterfacesIncludeSelf(typeof(T)).
        //        FirstOrDefault(t => t.IsDefined(typeof(ExcludePropertyAttribute), false));

        //    if (null != firstExcludeInterface)
        //        ExceptProperty(properties);
        //    else
        //        IncludeProperty(properties);
        //}
    }

    public class UmcListDesigner<T> : UmcDesigner<T>
        where T : IUmc
    {
        protected override List<string> ExceptionList
        {
            get
            {
                var source = base.ExceptionList;
                source.Add("Items");
                source.Add("Columns");
                return source;
            }
        }
    }

    //public class UmcTagNameAttribute : Attribute
    //{
    //    public UmcTagNameAttribute(string tagName)
    //    {
    //        TagName = tagName;
    //    }
    //    public string TagName { get; set; }
    //}
}
